// pages/index/index.js

import request from '../../utils/request.js'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerList: [],
        recommendList: [],
        topList: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this.getBannerList();
        this.getRecommendList();
        this.getTopList();
    },


    async getBannerList(){
        let res = await request('/banner', {type: 2})
        // console.log(res.banners);
        this.setData({
            bannerList: res.banners
        })
        // console.log(this.data.bannerList);
    },
    async getRecommendList(){
        let res = await request('/personalized', {limit: 20})
        // console.log(res);
        this.setData({
            recommendList: res.result
        })
        // console.log(this.data.recommendList);
    },


    async getTopList() {
        let index = 0;
        let resultArr = [];

        while (index < 5) {
            let topListData = await request('/top/list', {idx: index++});
            // console.log(topListData);
            let topListItem = {name: topListData.playlist.name, tracks: topListData.playlist.tracks.slice(0, 3)};
            resultArr.push(topListItem);
            this.setData({
                topList: resultArr
            })
        }
        // this.setData({
        //     topList: resultArr
        // })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})